package liuyao.webutils.controller;

import liuyao.webutils.utils.HttpUtils;
import liuyao.webutils.utils.ZipCreate;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/reptile")
public class ReptileController {

    private static final String[][] PAGE_STATIC = {
            {"link", "href"},
            {"script", "src"},
            {"img", "src"},
            {"img", "data-original"},
    };

    @GetMapping("page")
    public String reptilePage(HttpServletRequest request, String url){
        return "reptile";
    }

    @GetMapping("grab")
    public void reptile(HttpServletRequest request, HttpServletResponse response,
                        String url, Integer timeout){
        if (null == url || url.isEmpty()) return;

        ZipCreate zip;
        try {
            zip = new ZipCreate(response, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        Map<String, Short> urls = new HashMap<>();

        byte[] html = httpSource(url, timeout);
        zip.addFile("page.html", html);

        Document doc = null;
        try {
            doc = Jsoup.parse(new String(html, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return;
        }
        for (String[] a: PAGE_STATIC) {
            for (Element e: doc.getElementsByTag(a[0])){
                String attr = e.attr(a[1]);
                urls.put(attr, null);
            }
        }
        urls.remove("");

        for (String src: urls.keySet()) {
            String _url = url;
            if (url.endsWith(".html")) {
                _url = url.substring(0, url.lastIndexOf("/") + 1);
            } else if (!url.endsWith("/")){
                _url += "/";
            }
            while (src.startsWith("./")) {
                src = src.replaceFirst("./", "");
            }
            byte[] ret = httpSource(_url + src, timeout);
            if (null != ret) {
                zip.addFile(src, ret);
            }
        }

        zip.response("page.zip");

        System.out.println("finished");
    }

    public byte[] httpSource(String url, Integer timeout){
        return HttpUtils.ajax(url, HttpUtils.Method.GET, timeout, null, null);
    }
}
